﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Web.UI;
using System.Data;
using HySoft.Cash.BaseClass;
using Huayou.Utils;

namespace PayRoller.Manage
{
    public class Index : Page, IRequiresSessionState
    {
        // Fields
        protected Button bnLogin;
        protected HtmlForm form1;
        protected HtmlInputText txt_userName;
        protected HtmlInputPassword txt_userPwd;

        // Methods
        protected void bnLogin_Click(object sender, EventArgs e)
        {
            if ((this.txt_userName.Value != "") && (this.txt_userPwd.Value != ""))
            {
                TuserinfoBN obn = new TuserinfoBN(this.Page);
                obn.QueryUsername(this.txt_userName.Value);
                DataTable list = obn.GetList();
                if (list.Rows.Count > 0)
                {
                    if (Cipher.EncryptMD5(this.txt_userPwd.Value).Equals(list.Rows[0]["Password"].ToString()))
                    {
                        base.Response.Cookies["UserSid"].Value = list.Rows[0]["Sid"].ToString();
                        base.Response.Cookies["UserNameA"].Value = list.Rows[0]["Username"].ToString();
                        base.Response.Cookies["UserRule"].Value = list.Rows[0]["RuleSid"].ToString();
                        base.Response.Cookies["UserSid"].Expires = DateTime.Now.AddHours(12.0);
                        base.Response.Cookies["UserNameA"].Expires = DateTime.Now.AddHours(12.0);
                        base.Response.Cookies["UserRule"].Expires = DateTime.Now.AddHours(12.0);
                        string commandText = "";
                        commandText = "select * from tSysRuleModule where RuleSid=" + list.Rows[0]["RuleSid"].ToString();
                        list = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
                        string str3 = ",";
                        for (int i = 0; i < list.Rows.Count; i++)
                        {
                            string str4 = list.Rows[i]["ModuleCode"].ToString();
                            str3 = str3 + str4 + ",";
                        }
                        base.Response.Cookies["UserPerm"].Value = str3;
                        base.Response.Cookies["UserPerm"].Expires = DateTime.Now.AddHours(12.0);
                        base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>window.location='Main.aspx';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Error Password!');</script>");
                    }
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('This Username Is Not Exist!');</script>");
                }
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Please Confirm Your Username and Password!');</script>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
